package cn.fl.mcr.service.impl;

import java.text.DecimalFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import cmm.mid.core.framework.dto.DataResultDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.IMcrWriteOffPushDetailService;
import cn.fl.mcr.transform.McrWriteOffPushDetailVOTransform;
import cn.fl.mcr.vo.McrWriteOffPushDetailDVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.dto.McrWriteOffPushDetailDTO;
import cn.lionbridgecapital.loan.model.po.McrWriteOffPushDetail;
import cn.lionbridgecapital.loan.model.vo.McrWriteOffPushDetailVO;
import cn.lionbridgecapital.loan.service.api.IMcrWriteOffPushDetailApiService;

/**
 * @Description: 核销推送明细 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class McrWriteOffPushDetailServiceImpl implements IMcrWriteOffPushDetailService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IMcrWriteOffPushDetailService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IMcrWriteOffPushDetailApiService mcrWriteOffPushDetailApiService;

	@Override
	public McrWriteOffPushDetailVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		McrWriteOffPushDetailDTO dto = null;//this.自定义ApiService.selectById(id);
		return McrWriteOffPushDetailVOTransform.toVO(dto);
	}

	@Override
	public Long add(McrWriteOffPushDetailVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.mcrWriteOffPushDetailApiService.add(McrWriteOffPushDetailVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.mcrWriteOffPushDetailApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(McrWriteOffPushDetailVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.mcrWriteOffPushDetailApiService.modify(McrWriteOffPushDetailVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		//return this.mcrWriteOffPushDetailApiService.searchListPage(page);
		//TODO 业务逻辑
		BaseLogUtils.info(logger, "McrWriteOffPushDetailServiceImpl", "searchListPage->核销推送参数:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		page = mcrWriteOffPushDetailApiService.searchMcrWriteOffPushDetailListPage(page);
		BaseLogUtils.info(logger, "McrWriteOffPushDetailServiceImpl.searchListPage", "核销推送返回--->:"+JSON.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		DecimalFormat df = new DecimalFormat("######0.00");
		List<McrWriteOffPushDetailDVO> voList = Lists.newArrayList();
		if(CollectionUtils.isNotEmpty(page.getResultList()) ) {
			List<McrWriteOffPushDetail> detailList = (List<McrWriteOffPushDetail>) page.getResultList();
			for(McrWriteOffPushDetail dto:detailList) {
				McrWriteOffPushDetailDVO vo = new McrWriteOffPushDetailDVO();
				BeanUtils.copyProperties(dto, vo);
				vo.setRepayMoney(df.format(DecimalUtils.convertFenToYuan(dto.getRepayMoney())));
				vo.setRepayAmt(df.format(DecimalUtils.convertFenToYuan(dto.getRepayAmt())));
				vo.setRepayPrincipal(df.format(DecimalUtils.convertFenToYuan(dto.getRepayPrincipal())));
				vo.setRepayInterest(df.format(DecimalUtils.convertFenToYuan(dto.getRepayInterest())));
				vo.setRepayPenaltyInterest(df.format(DecimalUtils.convertFenToYuan(dto.getRepayPenaltyInterest())));
				voList.add(vo);
			}
		}
		page.setResultList(voList);
		BaseLogUtils.info(logger, "McrWriteOffPushDetailServiceImpl.searchListPage", "核销推送转vo返回--->:"+JSON.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return page;
	}

	@Override
	public DataResultDTO executeAgainPushWriteOffConfirmSummar(String batNo) {
		DataResultDTO resDto = mcrWriteOffPushDetailApiService.executePushWriteOffConfirmSummar(batNo);
		return resDto;
	}
}
